﻿using HDS.Admin.Api.ViewModels.ExchangeRate;
using HDS.IService.Master;
using HDS.Model;
using HDS.Model.Entity.Master;
using LinqKit;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;

namespace HDS.Admin.Api.Controllers
{
    [Route("exchangerate/[action]")]
    [ApiController]
    [EnableCors]
    [Authorize]
    public class ExchangeRateController : AdminController
    {
        private readonly ILogger<ExchangeRateController> _logger;
        private readonly IExchangeRateService _iechangeRateService;

        public ExchangeRateController(ILogger<ExchangeRateController> logger, IExchangeRateService iechangeRateService)
        {
            _logger = logger;
            _iechangeRateService = iechangeRateService;
        }

        public async Task<ResultData<DataPagingModel<ExchangeRate>>> GetList([FromBody] SearchDto searchDto)
        {
            var predicate = PredicateBuilder.New<ExchangeRate>(true);
            if (!string.IsNullOrEmpty(searchDto.Code))
                predicate = predicate.And(e => e.CodeFrom.Equals(searchDto.Code) || e.CodeTo.Equals(searchDto.Code));

            var dataPaging = await _iechangeRateService.GetList(predicate, searchDto.PageIndex, searchDto.PageSize);

            return new ResultData<DataPagingModel<ExchangeRate>> { Success = true, Data = dataPaging };
        }

        public async Task<ResultData<string>> Update([FromBody] ExchangeRate entity)
        {
            entity.UpdatedBy = this.UserId;
            entity.UpdatedByName = this.UserName;
            entity.UpdatedTime = DateTime.Now;
            await _iechangeRateService.Update(entity);
            return new ResultData<string> { Success = true };
        }

        public async Task<ResultData<string>> Delete(string CodeFrom, string CodeTo)
        {
            await _iechangeRateService.Delete(CodeFrom, CodeTo);
            return new ResultData<string> { Success = true };
        }
    }
}
